<?php

class Menu_controller extends CI_Controller{

		function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
                            
        Validator::is_access($this->session->userdata('user_id'));
    }

    function index(){

        $section = "menu";
        $title = _LB_MENU;
        $cfer = new Cfer(array(
        $title => base_url('menu')));
        
        $filter = array();
        $filter['name'] = $this->input->post('name');
        $menu = Menu::getList($filter);

        $this->data['menu'] = $menu;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->load->view('menu_list', $this->data);
    }
    
    function form($id = null) {
        $section = "menu";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_MENU => 'menu',
            _LB_EDIT_MENU => base_url('menu/form/'.$id)));
        }
        
        $act = $this->input->get_post('act');
    
        $menu = new Menu();

        if ($id && !$menu->get($id)) {
                redirect(base_url('menu'));
        }
        
        if ($act == ACT_VALID_EDIT) {

            $menu->permission = $this->input->post('group');
            if ($menu->validateInput()) {
                $menu->update();
                redirect(base_url('menu'));
            }
        }

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $title = _LB_MENU;
        
        $this->data['menu'] = $menu;
        $this->data['title'] = $title;
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('menu');
        $this->data['section'] = $section;
        $this->data['group'] = Group::getList();
        $this->data['group_ids'] = explode(",", $menu->permission);
        $this->data['cfer'] = $cfer;
        $this->load->view('menu_form', $this->data);
    }

}
